package com.xxx.common.service.serviceImpl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.xxx.common.entity.Entrust;
import com.xxx.common.entity.Project;
import com.xxx.common.entity.User;
import com.xxx.common.mapper.EntrustMapper;
import com.xxx.common.mapper.ProjectMapper;
import com.xxx.common.mapper.UserMapper;
import com.xxx.common.service.EntrustService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xxx.common.utils.TimeUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 */
@Service
public class EntrustServiceImpl extends ServiceImpl<EntrustMapper, Entrust> implements EntrustService {

    @Autowired
    private EntrustMapper entrustMapper;
    @Autowired
    private ProjectMapper projectMapper;
    @Autowired
    private UserMapper userMapper;

    @Override
    public PageInfo<Entrust> selectEntrustInfo(Entrust entrust, Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum,pageSize);
        List<Entrust> data = entrustMapper.selectList2(entrust);
        for (Entrust entrust1:data){
            String maintainIds = entrust1.getMaintainIds();
            String partIds = entrust1.getPartIds();
            List<Project> maintainList = new ArrayList<>();
            List<Project> partList = new ArrayList<>();
            if (maintainIds!=null && !maintainIds.equals("")){
                String[] s1 = maintainIds.split(",");
                for (String id:s1){
                    Project project = projectMapper.selectById(Integer.valueOf(id));
                    if (project !=null){
                        maintainList.add(project);
                    }
                }
            }
            if (partIds!=null && !partIds.equals("")){
                String[] s1 = partIds.split(",");
                for (String id:s1){
                    Project project = projectMapper.selectById(Integer.valueOf(id));
                    if (project !=null){
                        partList.add(project);
                    }
                }
            }
            entrust1.setMaintainList(maintainList);
            entrust1.setPartList(partList);
            if (entrust1.getTeUserId() !=null){
                User user = userMapper.selectById(entrust1.getTeUserId());
                if (user !=null){
                    entrust1.setTeRealName(user.getRealName());
                }else {
                    entrust1.setTeRealName("暂无技师");
                }
            }else {
                entrust1.setTeRealName("暂无技师");
            }
        }
        return new PageInfo<>(data);
    }

    @Override
    public Integer saveAndUpdateEntrustInfo(Entrust entrust) {
        if (entrust.getId() == null){
            entrust.setStatus("1");
            entrust.setNumber(TimeUtils.getNumber());
            return entrustMapper.insert(entrust);
        }else {
            if (entrust.getStatus() !=null && entrust.getStatus().equals("6")){
                //返工
                entrust.setStatus("1");
                entrust.setTeUserId("-1");
            }
            return entrustMapper.updateById(entrust);
        }
    }

    @Override
    public Integer delEntrustInfo(Integer id) {
        return entrustMapper.deleteById(id);
    }
}
